package com.dmgis.qxfw.dataservice.businessservice.yyhx.controller;

import com.dmgis.qxfw.dataservice.businessservice.api.domain.DbConfig;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.zy.base_product;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.zy.ccshx_imp_dot;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.zy.cshx_zsyb;
import com.dmgis.qxfw.dataservice.businessservice.common.util.DateTime;
import com.dmgis.qxfw.dataservice.businessservice.common.util.FileUtil;
import com.dmgis.qxfw.dataservice.businessservice.common.util.StringUtil;
import com.dmgis.qxfw.dataservice.businessservice.dmgis_mongodb_srv.dataservice.service.Impl.FileRequestService;
import com.dmgis.qxfw.dataservice.businessservice.dmgis_mongodb_srv.dataservice.service.Impl.MongoService1;
import com.dmgis.qxfw.dataservice.businessservice.yyhx.service.IFirefcstService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/*气象信息预报*/
@Controller
@RequestMapping("/xthx")
public class FirefcstController {
	private  final String operatingSystem;

	@Autowired
	public FirefcstController(String operatingSystem) {
		this.operatingSystem = operatingSystem;
	}
	@Resource
	private IFirefcstService firefcstService;

	/* 日志记录 */
	private static Logger log = Logger.getLogger(FirefcstController.class);

	//MongoService1 ms = new MongoService1();
	FileRequestService ms = new FileRequestService();

	/**
	 * 初始化火险指数
	 */
	@RequestMapping("/initcshxPage")
	@ResponseBody
	public Object initcshxPage() {
		ArrayList<Object> arrayList = new ArrayList<Object>();
		cshx_zsyb newestDate = firefcstService.initcshxPage();
		// 加载列表
		String forcastdate = new DateTime(newestDate.getForcastdate()).toDateString();
		arrayList.add(forcastdate);
		return arrayList;
	}
	
	/**
	 * 获取时间点的指数查看
	 * @param request
	 * @param model
	 * @return
	 * @throws ParseException
	 */
	@RequestMapping("/getFireIndex")
	@ResponseBody
	public Object getFireIndex(HttpServletRequest request, Model model) throws ParseException {
		String date = request.getParameter("date");
		List<cshx_zsyb> resultList = firefcstService.getFireIndex(date);
		for(cshx_zsyb zsyb : resultList){
			zsyb.setDate(new DateTime(zsyb.getForcastdate()).toDateString());
			List<ccshx_imp_dot> result = firefcstService.getImpdot(zsyb.getStationname());
			String names = "";//名称
			String principals = "";//负责人
			String tels = "";//联系电话
			for(ccshx_imp_dot dot : result){
				names += dot.getName() + ",";
				principals += dot.getPrincipal() + ",";
				tels += dot.getTel() + ",";
			}
			if(!StringUtil.IsNullOrEmpty(names)){
				names = names.substring(0, names.length()-1);
				principals = principals.substring(0, principals.length()-1);
				tels = tels.substring(0, tels.length()-1);
			}
			zsyb.setName(names);
			zsyb.setPrincipal(principals);
			zsyb.setTel(tels);
		}
		return resultList;
	}

	/**
	 * 获取时间点的火险文档
	 * @param request
	 * @param model
	 * @return
	 * @throws ParseException
	 */
	@RequestMapping("/showDoc")
	@ResponseBody
	public Object showDoc(HttpServletRequest request, Model model) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy年MM月dd日");
		String date = request.getParameter("date");
		Date parse = sdf.parse(date);
		String date1 = sdf1.format(parse);
		String strName = date1 + "城市火险等级预报产品";
		String cpName = strName + ".docx";
		List<base_product> resultList = firefcstService.showDoc(cpName);
		String servicePath = "";// 服务器文件
		for(base_product product : resultList){
			String id = product.getId().toString();// 获取编号
			String urlpath ;
			if (operatingSystem.equals("Windows")){
				 urlpath = request.getSession().getServletContext().getRealPath("/") + "Temp\\CityFire\\";// 临时文件目录
			}else {
				 urlpath = request.getSession().getServletContext().getRealPath("/") + "Temp/CityFire/";// 临时文件目录
			}

			String path = request.getContextPath();
			String basePath = DbConfig.IP + path;
			FileUtil.mkDirs(urlpath);// 创建文件夹，生成多级目录
			FileUtil.ClearTempFiles(urlpath, 1000);// 清除缓存

			String fileName = cpName;// 文件名
			String doctype = fileName.split("\\.")[fileName.split("\\.").length - 1];// 文件类型
			if (doctype.contains("doc")) {
				String pdfPath = urlpath + fileName.replace(doctype, "pdf");
				File pdfFile = new File(pdfPath);
				if (pdfFile.exists()) {
					if (operatingSystem.equals("Windows")){
						servicePath = "\\Temp\\CityFire\\" + fileName.replace(doctype, "pdf");
					}else {
						servicePath = "/Temp/CityFire/" + fileName.replace(doctype, "pdf");
					}

					servicePath = basePath+servicePath;
				} else {
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("id", id);
					map.put("table_name", "base_product");
					map.put("collectionName", "pdf_file");
					map.put("advCode", DbConfig.advCode);
					byte[] data = ms.selectData(map);
					// 下载doc文件
					if (data == null) {
						return "";
					}
					FileUtil.bytesToFile(data, pdfPath);
					if (operatingSystem.equals("Windows")){
						servicePath = "\\Temp\\CityFire\\" + fileName.replace(doctype, "pdf");
					}else {
						servicePath = "/Temp/CityFire/" + fileName.replace(doctype, "pdf");
					}

					servicePath = basePath+servicePath;
				}
			}
			servicePath = servicePath.replace("\\", "/");
		}
		return servicePath;
	}
	
}